package com.viettel.data;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;

import com.viettel.BO.RecruitmentAnouncementBean;

public class RecruitmentAnouncementFetcher {

	private final String NAMESPACE = "http://webservice.viettel.com/";
	private final String METHOD_NAME = "getRecruitmentAnouncement";
	private final String SOAP_ACTION = "http://webservice.viettel.com/getRecruitmentAnouncement";
	private final String URL = "http://192.168.5.101:8084/RMSPortal/RMSWebService?wsdl";

	private SoapSerializationEnvelope envelope;
	private ArrayList<RecruitmentAnouncementBean> listResult;

	public RecruitmentAnouncementFetcher(Long id, String keyword) {
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo quotesProperty = new PropertyInfo();
		quotesProperty.setName("userName");
		quotesProperty.setValue("userName");
		quotesProperty.setType(String.class);
		request.addProperty(quotesProperty);

		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);

//		new Connection().execute();
	}

	private class Connection extends AsyncTask {
		@Override
		protected Object doInBackground(Object... arg0) {
			getList();
			return listResult;
		}
	}

	public ArrayList<RecruitmentAnouncementBean> getList() {
		HttpTransportSE httpRequest = new HttpTransportSE(URL);
		RecruitmentAnouncementBean entity = new RecruitmentAnouncementBean();
		listResult = new ArrayList<RecruitmentAnouncementBean>();
		try {
			httpRequest.call(SOAP_ACTION, envelope);
			SoapObject response2 = (SoapObject) envelope.bodyIn;
			int iToltalCount = response2.getPropertyCount();
			if (iToltalCount > 0) {
				SoapObject element;
				for (int i = 0; i < iToltalCount; i++) {
					element = (SoapObject) response2.getProperty(i);
					entity = new RecruitmentAnouncementBean();
					if (element.hasProperty("recruitmentAnouncementId")
							&& element.getProperty("recruitmentAnouncementId") != null) {
						entity.setRecruitmentAnouncementId(Long
								.parseLong(element.getProperty(
										"recruitmentAnouncementId").toString()));
					}
					if (element.hasProperty("positionName")
							&& element.getProperty("positionName") != null) {
						entity.setPositionName(element.getProperty(
								"positionName").toString());
					}
					if (element.hasProperty("organizationName")
							&& element.getProperty("organizationName") != null) {
						entity.setOrganizationName(element.getProperty(
								"organizationName").toString());
					}
					if (element.hasProperty("locationName")
							&& element.getProperty("locationName") != null) {
						entity.setLocationName(element.getProperty(
								"locationName").toString());
					}
					if (element.hasProperty("jobDescription")
							&& element.getProperty("jobDescription") != null) {
						entity.setJobDescription(element.getProperty(
								"jobDescription").toString());
					}
					if (element.hasProperty("jobRequire")
							&& element.getProperty("jobRequire") != null) {
						entity.setJobRequire(element.getProperty("jobRequire")
								.toString());
					}
					if (element.hasProperty("globalRequire")
							&& element.getProperty("globalRequire") != null) {
						entity.setGlobalRequire(element.getProperty(
								"globalRequire").toString());
					}
					// if (element.getProperty("endDate") != null) {
					// entity.setEndDate(element.getProperty(
					// "endDate").toString());
					// }
					if (element.hasProperty("quantity")
							&& element.getProperty("quantity") != null) {
						entity.setQuantity(Long.parseLong(element.getProperty(
								"quantity").toString()));
					}
					if (element.hasProperty("otherRequire")
							&& element.getProperty("otherRequire") != null) {
						entity.setOtherRequire(element.getProperty(
								"otherRequire").toString());
					}
					if (element.hasProperty("address")
							&& element.getProperty("address") != null) {
						entity.setAddress(element.getProperty("address")
								.toString());
					}
					if (element.hasProperty("recruitmentName")
							&& element.getProperty("recruitmentName") != null) {
						entity.setRecruitmentName(element.getProperty(
								"recruitmentName").toString());
					}
					if (element.hasProperty("type")
							&& element.getProperty("type") != null) {
						entity.setType(element.getProperty("type").toString());
					}
					if (element.hasProperty("interest")
							&& element.getProperty("interest") != null) {
						entity.setInterest(element.getProperty("interest")
								.toString());
					}
					listResult.add(entity);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listResult;
	}

	public ArrayList<RecruitmentAnouncementBean> getListResult() {
		return listResult;
	}

	public void setListResult(ArrayList<RecruitmentAnouncementBean> listResult) {
		this.listResult = listResult;
	}
}
